﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using KMF.Common;

namespace KMF.DAL
{
    public partial class ProductCommentDb
    {
        public static List<ProductCommentDto> GetAllProductCommentWithPagging(int page, int itemsPerPage, ref int totalPage)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_Admin_ProductComment_GetAllPaging");
            try
            {
                command.Parameters.Add(dao.CreateParameter("@Page", page));
                command.Parameters.Add(dao.CreateParameter("@ItemsPerPage", itemsPerPage));
                SqlParameter itemTotalPage = command.Parameters.Add("@TotalPage", SqlDbType.Int);
                itemTotalPage.Direction = ParameterDirection.Output;

                var result = dao.GetDTOList<ProductCommentDto>(ref command);
                totalPage = int.Parse(itemTotalPage.Value.ToString() == "" ? "0" : itemTotalPage.Value.ToString());
                return result;
            }
            finally
            {
                command.Connection.Close();
            }
        }

        public static List<ProductCommentDto> GetProductCommentByProductId(string productId)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_ProductComment_GetByProductId");
            try
            {
                command.Parameters.Add(dao.CreateParameter("@ProductId", productId));
                return dao.GetDTOList<ProductCommentDto>(ref command);
            }
            finally
            {
                command.Connection.Close();
            }
        }

        public static int UpDateActive_ProductComment(int pCommentId, bool Agree, bool Active)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_ProductComment_UpDateActive");
            try
            {
                command.Parameters.Add(dao.CreateParameter("@ProductCommentId", pCommentId));
                command.Parameters.Add(dao.CreateParameter("@Agree", Agree));
                command.Parameters.Add(dao.CreateParameter("@Active", Active));
                return command.ExecuteNonQuery();
            }
            finally
            {
                command.Connection.Close();
            }
        }
    }
}
